package com.springboot.fram.core.lang;

import java.util.List;

/**
 * @author liheng
 * @ClassName Utils
 * @Description
 * @date 2020-10-12 10:08
 */
public class Utils {

    /**
     * long 安全的 转 int
     *
     * @param l
     * @return
     */
    public static int safeLongToInt(long l) {
        if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
            throw new IllegalArgumentException
                    (l + " cannot be cast to int without changing its value.");
        }
        return (int) l;
    }

    /**
     * 判断是否在允许范围内的后缀
     * 多个以“|”隔开
     *
     * @param allowExt 允许的后缀
     * @param ext      需要验证的后缀
     * @return 是否允许
     */
    public static boolean checkExt(String allowExt, String ext) {
        if (ext.length() == 0) {
            return false;
        }
        String[] params = allowExt.split("\\|");
        for (int i = 0; i < params.length; i++) {
            if (params[i].toLowerCase().equals(ext.toLowerCase())) {
                return true;
            }
        }
        return false;
    }


    /**
     * 产生0,1的所有组合
     * http://www.cocoachina.com/articles/91961
     * Tools.zeroOneGen(dataArrTemp, 0, tempResult)
     *
     * @param dataArr 数组
     * @param index
     */
    public static List<String> zeroOneGen(int[] dataArr, int index, List<String> results) {
        /**
         * 只输出 0 1组合
         */
        if (index == dataArr.length) {
            String str = "";
            for (int i = 0; i < dataArr.length; i++) {
                str = str + dataArr[i];
            }
            results.add(str);
        } else {
            for (int i = 0; i < 2; i++) {
                dataArr[index] = i;
                zeroOneGen(dataArr, index + 1, results);
            }
        }
        return results;
    }
}
